package com.jichangxiu.server.platform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.framework.mybatis.BaseServiceImpl;
import com.jichangxiu.server.platform.entity.dto.PlatformLoginQueryDto;
import com.jichangxiu.server.platform.entity.map.PlatformLoginBasic;
import com.jichangxiu.server.platform.entity.po.PlatformLogin;
import com.jichangxiu.server.platform.entity.vo.PlatformLoginQueryVo;
import com.jichangxiu.server.platform.mapper.PlatformLoginMapper;
import com.jichangxiu.server.platform.service.PlatformLoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * PlatformLoginServiceImpl:
 *
 * @author Jcx
 * @create 2023-02-26 20:57
 */
@Service
public class PlatformLoginServiceImpl extends BaseServiceImpl<PlatformLoginMapper, PlatformLogin> implements PlatformLoginService {

    @Resource
    private StrUtils strUtils;

    @Resource
    private PlatformLoginMapper platformLoginMapper;

    /**
     * 保存登录日志
     *
     * @param record 登录日志信息载体
     * @return 是否成功
     */
    @Override
    public Boolean saveLoginInfo(Record record) {
        PlatformLogin platformLogin = record.getModel(PlatformLogin.class);
        platformLogin.setLoginNo(strUtils.createNo("login"));
        return save(platformLogin);
    }

    /**
     * 分页登录日志列表
     *
     * @param platformLoginQueryVo 登录日志对象
     * @return 分页登录日志列表
     */
    @Override
    public PageData<PlatformLoginQueryDto> pageList(PlatformLoginQueryVo platformLoginQueryVo) {
        Page<PlatformLogin> page = startPage();
        List<PlatformLogin> platformLoginList = platformLoginMapper.pageList(PlatformLoginBasic.INSTANCE.toPlatformLogin(platformLoginQueryVo), page);
        List<PlatformLoginQueryDto> platformLoginQueryDtoList = PlatformLoginBasic.INSTANCE.toPlatformLoginQueryDtoList(platformLoginList);
        return PageData.build(new Page<>(page.getCurrent(), page.getSize(), page.getTotal()), platformLoginQueryDtoList);
    }

}
